//
//  GQHShipperAuthorizationInfoView.m
//  Xanthium
//
//  Created by GuanQinghao on 31/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHShipperAuthorizationInfoView.h"
#import "GQHCustomCell.h"
#import "GQHHeader.h"
#import <UIImageView+WebCache.h>
#import "YHPhotoBrowser.h"

@interface GQHShipperAuthorizationInfoView () <UITableViewDelegate,UITableViewDataSource> {
    
    NSArray *titleArray;
}

@end


@implementation GQHShipperAuthorizationInfoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

#pragma mark --Data
- (void)loadLocalData {
    
    titleArray = @[@"公司名称",@"公司地址",@"联系人",@"联系电话",@"身份证号码",@"营业执照号码",@"企业介入码",@"企业接入AppKey",@"社会信用代码",@"本人正面照",@"身份证正面",@"身份证反面",@"门面照片",@"营业执照照片",@"道路运输许可证",@"企业纳税凭证"];
}

#pragma mark --View
- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.and.bottom.mas_equalTo(self);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row > 8) {
        
        return 60.0f;
    }
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row > 8) {
        
        static NSString *identifier = @"aCell";
        GQHTruckImagesCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHTruckImagesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.qh_captionLabel.text = titleArray[indexPath.row];
        cell.qh_leftImageView.hidden = YES;
        NSString * urlString = [[NSString qh_APIURLString] stringByAppendingPathComponent:_qh_contentArray[indexPath.row]];
        UIImage *image = [UIImage qh_imageWithColor:kLightGrayColor];
        [cell.qh_rightImageView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:image];
        cell.qh_rightImageView.userInteractionEnabled = YES;
        cell.qh_rightImageView.tag = indexPath.row + 10;
        
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewImageAction:)];
        [cell.qh_rightImageView addGestureRecognizer:tap];
        
        return cell;
    } else {
        
        static NSString *identifier = @"bCell";
        GQHCustomTextDoubleHorizontalCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHCustomTextDoubleHorizontalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.qh_leftLabel.text = titleArray[indexPath.row];
        cell.qh_rightLabel.text = _qh_contentArray[indexPath.row];
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)viewImageAction:(UITapGestureRecognizer *)tap
{
    NSInteger tag = tap.view.tag - 10;
    NSString * urlString = [[NSString qh_APIURLString] stringByAppendingPathComponent:_qh_contentArray[tag]];
    CGRect rect1 = [tap.view convertRect:tap.view.frame fromView:tap.view.superview];//获取imagevuew在subview的位置
    CGRect rect2 = [tap.view convertRect:rect1 toView:self];
    
    YHPhotoBrowser *photoView = [[YHPhotoBrowser alloc]init];
    photoView.sourceView = self;//图片所在的父容器
    photoView.urlImgArr = @[urlString]; //网络链接图片的数组
    photoView.sourceRect = rect2; // 图片的frame
    photoView.indexTag = 0;   //初始化进去显示的图片下标
    [photoView show];
}

@end
